package com.ruoyi.system.dao;

import com.ruoyi.common.core.domain.entity.SysCorp;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Repository
public interface SysCorpDAO extends JpaRepository<SysCorp, String>, JpaSpecificationExecutor<SysCorp>, Serializable {

    public SysCorp findTop1ByCorpName(String corpName);


    public List<SysCorp> findByCorpName(String corpName);

    public SysCorp findTop1ByCorpCode(String corpCode);


    public List<SysCorp> findByCorpCode(String corpCode);


    @Transactional
    @Modifying
    @Query(value = " update sys_corp set enablestate = ?2 where pk_corp = ?1 ", nativeQuery = true)
    public int updateCorpEnableState(String pkCorp,String enablestate);


    @Query(value = "select distinct c.* from sys_corp c "
            + " inner join sys_role_org ro on ro.org_id = c.pk_corp "
            + " inner join sys_role_user ur on ur.role_id = ro.role_id "
            + " inner join sys_role r on ro.role_id = r.role_id "
            + " WHERE r.del_flag = '0' and ur.user_id = ?1  ", nativeQuery = true)
    public List<SysCorp> selectRoleCorpByUserId(String userId);
}
